package com.fdhwws.web.dao.impl;

import com.fdh.myjdbcutil.MyJdbcUtil;
import com.fdhwws.web.dao.UserDao;
import com.fdhwws.web.domain.User;

import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public User selectById(Integer id) {
        return MyJdbcUtil.getInstance()
                .selectSql_upgrade("SELECT * FROM user WHERE id = ?",
                        new Object[]{id},
                        User.class);
    }

    @Override
    public List<User> selectAll() {
        return MyJdbcUtil.getInstance()
                .selectAll_upgrade("SELECT * FROM user", User.class);
    }

    @Override
    public int insert(User entity) {
        return MyJdbcUtil.getInstance()
                .insertSql_upgrade(entity);
    }

    @Override
    public int update(User entity) {
        return MyJdbcUtil.getInstance()
                .updateSql_upgrade(entity);
    }

    @Override
    public int delete(Integer id) {
        return MyJdbcUtil.getInstance()
                .updateSql("DELETE FROM user WHERE id = ?", new Object[]{id});
    }

    @Override
    public User selectByUsername(String username) {
        return MyJdbcUtil.getInstance()
                .selectSql_upgrade("SELECT * FROM user WHERE username = ?",
                        new Object[]{username},
                        User.class);
    }

    @Override
    public User selectByUsernameAndPassword(String username, String password) {
        return MyJdbcUtil.getInstance()
                .selectSql_upgrade("SELECT * FROM user WHERE username = ? AND password = ?",
                        new Object[]{username, password},
                        User.class);
    }
}
